﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace A.HTMlServer
{
    public class CrawlArticle
    {
        ArticleModel model = null;
        List<string> Lstr = new List<string>();
        string TempUrl = null;
        DCommon dc;

        public CrawlArticle()
        {
        }

        public ArticleModel ToPage(HListModel Hmodel)
        {
            string url = Hmodel.Url;
            if (!Lstr.Contains(url))
            {
                Lstr.Add(url);
                TempUrl = url.Substring(0, url.LastIndexOf(".html"));
                dc = new DCommon();
                string Html = dc.GetStrHtml(Hmodel.Url);
                if (Html != null)
                {
                    model = new ArticleModel();
                    model.Title = ToTitle(Html);

                    model.Content = ToContent(Html);
                    model.page = GetPage(Html);
                    model.Introduction = ToIntroduction(Html);
                    other(Html);
                    model.Code = Hmodel.Other;
                    model.CodeName = Hmodel.Code;
                    return model;
                }
            }
            return null;
        }
        private void other(string Html)
        {
            string strHtml=GetPartHtml(Html, "<div class=\"text\">(.*?)</div>");
            if(strHtml=="")
            {
                strHtml=GetPartHtml(Html, "<span class=\"info\">(.*?)<p class=\"summary\">");
            }
            if (strHtml != "")
            {
                Match m= Regex.Match(strHtml, @"(\d{4})\D*(\d{2})\D*(\d{2})(\D*(\d{2})\D*(\d{2})\D*(\d{2}))?");
                DateTime dt;
                if (m.Success&&DateTime.TryParse(m.Value,out dt))
                {
                    model.Date = dt;
                }
                else
                {
                    model.Date = DateTime.Now;
                }
                string strfrom = GetPartHtml(strHtml, @"来源.*?>(.*?)</a>");
                if (strfrom != "")
                {
                    model.From = strfrom;
                }
                else
                {
                    model.From = "网易女人";
                }
            }
        }

        private string ToIntroduction(string Html)
        {
            if (Html.Contains("endSummary"))
            {
                return GetPartHtml(Html, "<div class=\"endSummary\">(.*?)</div>");
            }
            else
            {
                return GetPartHtml(Html, "<p class=\"summary\">(.*?)</p>");
            }
        }

        private string ToTitle(string Html)
        {
            return GetPartHtml(Html, "<h1.*?>(.*?)</h1>");
        }
        private string ToContent(string Html)
        {
            string Content = GetPartHtml(Html, "<div id=\"endText\">(.*?本文来源：.*?\\)).*?</div>.*?<!-- 分页 -->");
            return RemoveA(Content);
        }

        /// <summary>
        /// 分页后的内容
        /// </summary>
        /// <param name="url">url</param>
        private void GetPageHTML(string url)
        {
            string Html = dc.GetStrHtml(url);
            if (Html != null)
            {
                model.Content+="<!--page-->"+ToContent(Html);
            }
        }

        private int GetPage(string Html)
        {
            string strHtml = GetPartHtml(Html,"<div class=\"endPageNum\">(.*?)<span class=\"blank12\"></span>");
            Regex r1 = new Regex(TempUrl+@"_\d+\.html", RegexOptions.IgnoreCase);
            MatchCollection MC = r1.Matches(Html);
            foreach (Match m in MC)
            {
                if (!Lstr.Contains(m.Value))
                {
                    Lstr.Add(m.Value);
                    GetPageHTML(m.Value);
                }
            }
            return MC.Count;
        }

        /// <summary>
        /// 获取所有采集URL
        /// </summary>
        /// <param name="Html">HTML</param>
        /// <returns>MainHTMl</returns>
        private string GetPartHtml(string Html,string strRegex)
        {
            Regex r = new Regex(strRegex, RegexOptions.Singleline);
            Match m = r.Match(Html);
            if (m.Success)
            {
                if (m.Groups.Count> 1)
                {
                    return m.Groups[1].Value;
                }
                return m.Value;
            }
            return "";
        }

        private string GetReplaceHtml(string Html, string strRegex)
        {
            return Regex.Replace(Html, strRegex, "");
        }

        private string GetChinese(string Html)
        {
            Regex r = new Regex(@"^[\u4e00-\u9fa5]+.*?[\u4e00-\u9fa5]+$", RegexOptions.IgnoreCase);
            Match m = r.Match(Html);
            if (m.Success)
            {
                return m.Value;
            }
            return "";
        }

        private string RemoveA(string Html)
        {
            //<a href=http://lady.163.com/special/00262E96/newtuibu.html>腿部</a>
            //Regex r = new Regex(@"<a href=http://.*?>.*?</a>", RegexOptions.Singleline);
            //r.Replace(Html, "");
            //return r.Replace(Html, "$2");
            return Regex.Replace(Html, @"<a.*?>(.*?)</a>","$1");
        }
    }
}
